import asyncio
import logging
import os
from openai import AsyncOpenAI
from agents import Agent, OpenAIChatCompletionsModel, Runner, set_tracing_disabled
from agents.mcp import MCPServer, MCPServerSse
import pdb

# 初始化变量
BASE_URL = "https://api.siliconflow.cn/v1"
API_KEY = os.getenv("API_KEY", "sk-0d9449d2355a4c63******")
MODEL_NAME = "Qwen/Qwen3-8B"

logging.basicConfig(level=logging.DEBUG)

async def run(mcp_server: MCPServer):

    # 初始化OpenAI客户端
    client = AsyncOpenAI(base_url=BASE_URL, api_key=API_KEY)
    set_tracing_disabled(disabled=True)
    agent = Agent(
        name="Assistant",
        instructions="Use the tools to get the tin of company.",
        mcp_servers=[mcp_server],
        model=OpenAIChatCompletionsModel(model=MODEL_NAME, openai_client=client),
    )
    
    message = "联通数据智能有限公司税号是多少?"
    print(f"Running: {message}")
    result = await Runner.run(starting_agent=agent, input=message)
    print('final_result:',result.final_output)

async def main():
    async with MCPServerSse(
        name="GetTin SSE Server",
        params={
            "url": "http://127.0.0.1:8081/sse",
        },
    ) as server:
        await run(server)

if __name__ == "__main__":
    asyncio.run(main())
